package dao1;

import java.util.ArrayList;
import java.util.Scanner;



public class Test {
	
	static PersonDao sta = new PersonDao();
	Scanner sc = new Scanner(System.in);

	public static void main(String[] args) {
		Test user = new Test();
		user.begin();
	}

	
	public void begin() {

		while (true) {
			System.out.println("================");
			System.out.println("学 生 档 案 管 理 系 统");
			System.out.println("  1-添加学生信息");
			System.out.println("  2-查看所有学生信息");
			System.out.println("  3-按姓名查看学生");
			System.out.println("  4-删除学生信息");
			System.out.println("  5-修改学生信息");
			System.out.println("  6-按专业查询");
			System.out.println("================");
			switch (Integer.parseInt(sc.nextLine())) {
			case 1:
				add();
				break;
			case 2:
				printAll();
				break;
			case 3:
				findStudent();
				break;
			case 4:
				delStudent();
				break;
			case 5:
				updStudent();
				break;
			case 6:
				findBySpeciality();
				break;
			default:
				System.out.println("按提示输入啊，贴咩！");
				break;
			}
			System.out.println("******是否退出系统?y/n*****");
			if ("y".equalsIgnoreCase(sc.nextLine()))
				break;
		}
	}

	public void add() {
		while (true) {
			addStudent();
			System.out.println("是否继续录入?y/n");
			if ("n".equalsIgnoreCase(sc.nextLine()))
				break;
		}
	}

	public void addStudent() {// 添加学生
		StudentModel model = new StudentModel();
		String id;
		do {
			System.out.println("输入学生学号：");
			id = sc.nextLine();
		} while (isIdExist(id));
		model.setId(id);

		System.out.println("输入学生姓名：");
		model.setName(sc.nextLine());

		System.out.println("输入学生性别：");
		model.setGender(sc.nextLine());

		System.out.println("输入学生专业：");
		model.setSpeciality(sc.nextLine());

		System.out.println("输入学生年龄：");
		model.setYear(Integer.parseInt(sc.nextLine()));
		try {
			sta.insert(model);
		} catch (Exception e) {
			e.printStackTrace();
		}
		// list.add(model);
	}

	public boolean isIdExist(String id) {
		StudentModel model = new StudentModel();
		model.setId(id);
		ArrayList<StudentModel> list = null;
		try {
			list = sta.selectList(model);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (list.size() == 0) {// 长度为0，不存在该id返回false
			System.out.println("该学号(" + id + ")不在表中");
			return false;
		}
		System.out.println("该学号(" + id + ")在表中");
		return true;
	}

	public void findStudent() {// 按姓名查询学生信息
		StudentModel model = new StudentModel();
		System.out.println("输入要查询学生姓名：");
		model.setName(sc.nextLine());
		ArrayList<StudentModel> list = new ArrayList<StudentModel>();
		list = getRecord(model);
		if (list.size() == 0) {
			System.out.println("没有该姓名学生！");
			return;
		} else {
			print(list);
		}
	}


	public void printAll() { // 显示所有学生信息
		ArrayList<StudentModel> list = new ArrayList<StudentModel>();
		StudentModel model = new StudentModel();
		list = getRecord(model);
		print(list);
	}

	public void findBySpeciality() {// 按专业查询
		StudentModel model = new StudentModel();
		System.out.println("输入专业名称：");
		model.setId(sc.nextLine());
		ArrayList<StudentModel> list = new ArrayList<StudentModel>();
		list = getRecord(model);
		if (list.size() == 0) {
			System.out.println("没有该专业学生！");
			return;
		} else {
			print(list);
		}
	}

	public void delStudent() {// 删除学生信息
		StudentModel model = new StudentModel();
		String id;
		do {
			System.out.println("输入要删除学生学号：");
			id = sc.nextLine();
		} while (!isIdExist(id));
		model.setId(id);
		System.out.println("是否删除？y/n");
		String s = sc.nextLine();
		if ("y".equals(s)) {
			try {
				sta.deleteById(model);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.out.println("已删除");
		} else {
			System.out.println("下次一定");
			return;
		}

	}

	public void updStudent() {// 修改学生信息
		StudentModel model = new StudentModel();
		String id;
		do {
			System.out.println("输入要修改学生学号：");
			id = sc.nextLine();
		} while (!isIdExist(id));
		model.setId(id);
		System.out.println("是否修改？y/n");
		String s = sc.nextLine();
		if ("y".equals(s)) {
			try {
				System.out.println("请进行修改操作：");
				System.out.println("输入学生姓名：");
				model.setName(sc.nextLine());

				System.out.println("输入学生性别：");
				model.setGender(sc.nextLine());

				System.out.println("输入学生专业：");
				model.setSpeciality(sc.nextLine());

				System.out.println("输入学生年龄：");
				model.setYear(Integer.parseInt(sc.nextLine()));
				sta.update(model);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.out.println("已修改");
		} else {
			System.out.println("下次一定");
			return;
		}

	}

	public ArrayList<StudentModel> getRecord(StudentModel model) { // 获取相应学生信息
		ArrayList<StudentModel> list = new ArrayList<StudentModel>();
		try {
			list = sta.selectList(model);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}
	public void print(ArrayList<StudentModel> list) {//打印列表
		System.out.println("学号   姓名   性别   年龄    专业");
		for (StudentModel model1 : list) {
			System.out.println(model1.getId() + "  " + model1.getName() + "  " + model1.getGender() + "  "
					+ model1.getYear() + "  " + model1.getSpeciality());
		}
	}
}
